//
//  ZGSDestinaDetailsViewController.m
//  Tour
//
//  Created by dllo on 15/10/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ZGSDestinaDetailsViewController.h"
#import "AdvanceEdit.h"
#import "ZGSDestionDetailsCell.h"
#import "ZGSDestinationCity.h"
#import "ZGSCountryViewController.h"
@interface ZGSDestinaDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)MBProgressHUD *HUD;
@end

@implementation ZGSDestinaDetailsViewController
- (void)dealloc{
    [_cityId release];
    [_collectionView release];
    [_arr release];
    [_HUD release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    UILabel *labelBack = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64
//    [self.view addSubview:labelBack];
//     Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 10, (HEIGHT - 64) / 3);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor cyanColor];
    [self.collectionView registerClass:[ZGSDestionDetailsCell class] forCellWithReuseIdentifier:@"reuseo"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.arr = [NSMutableArray array];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.dimBackground = YES;

    [self creatData];
}
- (void)creatData{
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@&lat=38.88266923325023&lon=121.5394170925639&page=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone7%%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%%25208.4.1&v=1", self.cityId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [ZGSDestinationCity baseModelArr:dic[@"data"]];
        self.HUD.hidden = YES;
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDestionDetailsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseo" forIndexPath:indexPath];
    cell.labelTItle.text = [self.arr[indexPath.row] representative];
    cell.labelName.text = [self.arr[indexPath.row] catename];
    cell.labelGo.text = [self.arr[indexPath.row] beenstr];
    [cell.imageUp sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
//    [self.collectionView reloadData];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZGSCountryViewController *zgsCountry = [[ZGSCountryViewController alloc] init];
    NSString *str = [NSString stringWithFormat:@"%@", [self.arr[indexPath.row] countryid]];
    zgsCountry.cityid = str;
    zgsCountry.name = [self.arr[indexPath.row] cnname];
    [self.navigationController pushViewController:zgsCountry animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
